/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    struct ListNode * firstPtr;
    struct ListNode * secondPtr;
    firstPtr = head;
    secondPtr = head;
    for (int i = 1; i < n; i++) 
        secondPtr = secondPtr->next;
    if (secondPtr->next == 0) {
        if (firstPtr == secondPtr) return 0;
        else 
            return head->next;
    }
    for (int i = 0; secondPtr->next != 0; i++) {
        secondPtr = secondPtr->next;
        if (secondPtr->next == 0) {
            firstPtr->next = firstPtr->next->next;
            return head;
        }
        firstPtr = firstPtr->next;
    }
    return head;
}